function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}
function shuffle(arr) {
  let _arr = arr.slice()
  for (let i = 0; i < _arr.length; i++) {
    let j = getRandomInt(0, i)
    let t = _arr[i]
    _arr[i] = _arr[j]
    _arr[j] = t
  }
  return _arr
}

export function existsStr(str, keys) {
	return str.indexOf(keys) > -1;
}

export function clone(obj) {
    let result = {}
    for(let key in obj){
        result[key] = obj[key]
    }
    return result
}

export function deepClone(obj) {
    let newObj = obj.constructor === Array ? []:{};
    if(typeof obj !== 'object'){
        return {}
    }else{
        for(var i in obj){
            if(obj.hasOwnProperty(i)){
                newObj[i] = typeof obj[i] === 'object'?deepClone(obj[i]):obj[i];
            }
        }
    }
    return newObj
}

export function encode(data) {
  return JSON.stringify(data)
}

export function decode(LString) {
  return JSON.parse(LString)
}

export function scrollTop(top=0,end=0){
    document.body.scrollTop = end
    document.documentElement.scrollTop = end
}


export function deepCopy(object) {
    if(object instanceof Object) {
        return JSON.parse(JSON.stringify(object));
    }else{
        return shuffle(object)
    }
}

export function getOffsetCoor(e) {
    let event = e || window.event;
    let offsetX = event.offsetX || event.layerX;
    let offsetY = event.offsetY || event.layerY;
    return {startX:offsetX, startY: offsetY};
}
export function getClientCoor(e) {
    let event = e || window.event;
    let clientX = event.clientX;
    let clientY = event.clientY;
    return {endX:clientX, endY:clientY};
}

export function rangeMap(value, in_min, in_max, out_min, out_max) {
    return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

export function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}

//计算bytes的大小
export function bytesToSize(bytes) {  
    if (bytes === 0){
     return '0 B'
    }
    let k = 1024, sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],i = Math.floor(Math.log(bytes) / Math.log(k))  
    return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}

export function countLength(str) {
  return str.replace(/[\u0391-\uFFE5]/g,"aa").length;
}